<template>
  <a-modal
    title="業主詳情"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <div class="main-content">
        <a-row>
            <a-col :span="12"><h2 class="zkxx">租客信息</h2></a-col>
            <a-col :span="12">
              <a-button @click="$refs.editZuKeForm.edit(form)" type="primary" class="item-end" ghost>
                修改
              </a-button>
            </a-col>
        </a-row>
        <a-row class="main-content-item">
          <a-col :span="24">業主姓名：{{ data.name }}</a-col>
          <a-col :span="24">聯繫方式：{{ data.phone }}</a-col>
        </a-row>
         <a-row class="main-content-item">
           <a-col :span="2" class="ydxx">相冊：</a-col>
           <a-col :span="4" v-for="(item, index) in fileList" :key="index" class="parImg">
              <a-icon type="delete" class="parImg_icon" @click="delImg(item.fileId)"/>
                 <img :src="item.url" style="max-width: 99%;width:100px"/>
            </a-col>
        </a-row>
      </div>
      <div class="main-content">
        <a-row>
            <a-col :span="12"><h2 class="zkxx">租約信息</h2></a-col>
            <a-col :span="12">
              <a-button @click="$refs.editZuKeForm.edit(form)" type="primary" class="item-end" ghost>
                修改
              </a-button>
            </a-col>
        </a-row>
        <a-row class="main-content-item">
          <a-col :span="24">業主姓名：{{ data.name }}</a-col>
          <a-col :span="24">聯繫方式：{{ data.phone }}</a-col>
        </a-row>
         <a-row class="main-content-item">
           <a-col :span="2" class="ydxx">相冊：</a-col>
           <a-col :span="4" v-for="(item, index) in fileList" :key="index" class="parImg">
              <a-icon type="delete" class="parImg_icon" @click="delImg(item.fileId)"/>
                 <img :src="item.url" style="max-width: 99%;width:100px"/>
            </a-col>
        </a-row>
      </div>
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="業主名稱"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入業主名稱" v-decorator="['name', {rules: [{required: true, message: '请输入業主名稱！'}]}]" />
        </a-form-item>
        <a-form-item
          label="手機號"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入手機號" v-decorator="['phone', {rules: [{required: true, message: '请输入手機號！'}]}]" />
        </a-form-item>
        <a-form-item
          label="租期开始时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择租期开始时间" v-decorator="['rentStartDate',{rules: [{ required: true, message: '请选择租期开始时间！' }]}]" @change="onChangerentStartDate"/>
        </a-form-item>
        <a-form-item
          label="租期结束时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择租期结束时间" v-decorator="['rentEndDate',{rules: [{ required: true, message: '请选择租期结束时间！' }]}]" @change="onChangerentEndDate"/>
        </a-form-item>
        <a-form-item
          label="付"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入付" v-decorator="['receiptFu', {rules: [{required: true, message: '请输入付！'}]}]" />
        </a-form-item>
        <a-form-item
          label="押"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入押" v-decorator="['receiptYa', {rules: [{required: true, message: '请输入押！'}]}]" />
        </a-form-item>
        <a-form-item
          label="依据"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入依据" v-decorator="['receiptYj', {rules: [{required: true, message: '请输入依据！'}]}]" />
        </a-form-item>
        <a-form-item
          label="租金"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="押金"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="账单起始日（0 起租日 1日，2日......） 默认为起租日"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入账单起始日（0 起租日 1日，2日......） 默认为起租日" v-decorator="['billStart', {rules: [{required: true, message: '请输入账单起始日（0 起租日 1日，2日......） 默认为起租日！'}]}]" />
        </a-form-item>
        <a-form-item
          label="账单收租日提前几日"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入账单收租日提前几日" v-decorator="['billEndTqData', {rules: [{required: true, message: '请输入账单收租日提前几日！'}]}]" />
        </a-form-item>
        <a-form-item
          label="开户银行"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择开户银行">
            <a-select-option v-for="(item,index) in cardBankTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="账号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入账号" v-decorator="['account']" />
        </a-form-item>
        <a-form-item
          label="收款人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入收款人" v-decorator="['payee']" />
        </a-form-item>
        <a-form-item
          label="收租提醒开关"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group placeholder="请选择收租提醒开关" v-decorator="['rentReminderSwitch',{rules: [{ required: true, message: '请选择收租提醒开关！' }]}]" >
            <a-radio v-for="(item,index) in rentReminderSwitchData" :key="index" :value="item.code">{{ item.name }}</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { ownerLeaseEdit } from '@/api/modular/main/ownerlease/ownerLeaseManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        rentStartDateDateString: '',
        rentEndDateDateString: '',
        cardBankTypeData: [],
        rentReminderSwitchData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        data: {},
        fileList: []
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        this.data = record
        const accountTypeOption = this.$options
        this.accountTypeData = accountTypeOption.filters['dictData']('account_type')
        const cardBankTypeOption = this.$options
        this.cardBankTypeData = cardBankTypeOption.filters['dictData']('card_bank_type')
        const rentReminderSwitchOption = this.$options
        this.rentReminderSwitchData = rentReminderSwitchOption.filters['dictData']('common_status')
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              name: record.name,
              phone: record.phone,
              receiptFu: record.receiptFu,
              receiptYa: record.receiptYa,
              receiptYj: record.receiptYj,
              rent: record.rent,
              deposit: record.deposit,
              billStart: record.billStart,
              billEndTqData: record.billEndTqData,
              cardBankType: record.cardBankType,
              account: record.account,
              payee: record.payee,
              rentReminderSwitch: record.rentReminderSwitch
            }
          )
        }, 100)
        // 时间单独处理
        if (record.rentStartDate != null) {
            this.form.getFieldDecorator('rentStartDate', { initialValue: moment(record.rentStartDate, 'YYYY-MM-DD') })
        }
        this.rentStartDateDateString = moment(record.rentStartDate).format('YYYY-MM-DD')
        // 时间单独处理
        if (record.rentEndDate != null) {
            this.form.getFieldDecorator('rentEndDate', { initialValue: moment(record.rentEndDate, 'YYYY-MM-DD') })
        }
        this.rentEndDateDateString = moment(record.rentEndDate).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.rentStartDate = this.rentStartDateDateString
            values.rentEndDate = this.rentEndDateDateString
            ownerLeaseEdit(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangerentStartDate(date, dateString) {
        this.rentStartDateDateString = dateString
      },
      onChangerentEndDate(date, dateString) {
        this.rentEndDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
        this.data = {}
      }
    }
  }
</script>
<style lang="less" scoped>
.zkxx{
  margin-left: 10px;
}
.item-end{
  float: right;
}
.main-content{
  margin-top: 10px;
  margin-bottom: 10px;
  border: solid 1px rgb(218, 211, 211);
}
.dj{
  border-bottom: solid 1px rgb(218, 211, 211);
}
.main-content-item{
  padding: 10px;
}
</style>
